package org.kinglone.back.system.action;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.hibernate.persister.collection.ElementPropertyMapping;
import org.kinglone.back.system.model.SystemParam;
import org.kinglone.back.system.service.SystemParamService;
import org.kinglone.common.util.JSONMessageView;
import org.kinglone.common.util.PageQuery;
import org.kinglone.common.util.PageResults;
import org.kinglone.common.util.StringUtil;
import org.springframework.remoting.RemoteTimeoutException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@Controller
@RequestMapping("/systemParam")
public class SystemParamAction {

	@Resource(name="systemParamService")
	private SystemParamService systemParamService;
	
	
	/**
	 * 跳转到系统参数配置后台分页页面
	 * @param request
	 * @return
	 */
	@RequestMapping("/systemParam.jspx")
	public ModelAndView system(HttpServletRequest request) {
		ModelAndView modelAndView = new ModelAndView("/WEB-INF/jsp/back/system/system.jsp");
		return modelAndView;
	}
	
	/**
	 * 跳转到修改参数页面
	 * @param request
	 * @return
	 */
	@RequestMapping("/updateSystemValue.jspx")
	public ModelAndView updateSystemValue(HttpServletRequest request) {
		ModelAndView modelAndView = new ModelAndView("/WEB-INF/jsp/back/system/updateSystemValue.jsp");
		String paramId = request.getParameter("paramId");
		SystemParam systemParam = systemParamService.get(paramId);
		request.setAttribute("systemParam",systemParam);
		return modelAndView;
	}
	
	/**
	 * 分页查询系统参数列表
	 * @param request
	 * @return
	 */
	@RequestMapping("/selSystemList.jspx")
	public JSONMessageView selSystemList(HttpServletRequest request){
		JSONMessageView json = new JSONMessageView(-1,"查询失败!",null);
		try {
			String pageNo = request.getParameter("pageNo");
			String pageSize = request.getParameter("pageSize");
			PageQuery pageQuery = new PageQuery();
			pageQuery.setStart((Integer.parseInt(pageNo)-1)*Integer.parseInt(pageSize));
			pageQuery.setLength(Integer.parseInt(pageSize));
			PageResults<SystemParam> list =systemParamService.findSystemParamByPage(pageQuery);
			JSONArray totalJsonArray = new JSONArray();
			JSONObject totalJsonObject = new JSONObject();
			JSONArray jsonArray=new JSONArray();
			for(SystemParam s : list.getResults()) {
				JSONObject jsonObject = new JSONObject();
				jsonObject.put("sysid", s.getSysid());
				jsonObject.put("paramName",s.getParamName());
				jsonObject.put("paramValue",s.getParamValue());
				jsonArray.add(jsonObject);
			}
			totalJsonObject.put("pagetotal", list.getPageCount());//总页数
			totalJsonObject.put("info", jsonArray);
			totalJsonArray.add(totalJsonObject);
			json.setCode(0);
			json.setMessage("查询成功!");
			json.setContent(totalJsonArray);
			return json;
		}catch (Exception e){
			e.printStackTrace();
			json.setCode(-1);
			json.setMessage("服务器异常,请稍后再试!");
			return json;
		}
	}
	
	/**
	 * 修改参数值
	 * @param request
	 * @return
	 */
	@RequestMapping("updateSystem.jspx")
	public JSONMessageView updateSystem(HttpServletRequest request) {
		JSONMessageView json = new JSONMessageView(-1,"修改失败!",null);
		try {
			String systemId = request.getParameter("systemId");
			String paramValue = request.getParameter("paramValue");
			if(StringUtil.isNullOrEmpty(paramValue)) {
				json.setCode(-1);
				json.setMessage("请输入参数值!");
				return json;
			}
			SystemParam systemParam = systemParamService.get(systemId);
			systemParam.setParamValue(paramValue);
			systemParamService.update(systemParam);
			json.setCode(0);
			json.setMessage("修改成功!");
			return json;
		} catch (Exception e) {
			e.printStackTrace();
			json.setCode(-1);
			json.setMessage("系统异常!");
			return json;
		}
	}
	
	
}
